import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useAnimateStore } from "../stores/animate";
import { RouterItem } from "./modules/index";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/dashboard",
  },
  {
    path: "/dashboard",
    name: "dashboard",
    component: () => import("../views/dashboard/index.vue"),
    children: RouterItem,
    redirect: "/dashboard/Home",
  },
  {
    path: "/:pathMatch(.*)",
    component: () => import("../views/404/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//使用路由拦截进行动画切换
let timer: any = null;
let timermon: any = null;
router.beforeEach((to, from, next) => {
  const animatestore = useAnimateStore && useAnimateStore();
  animatestore && animatestore.SetAnimate(false);
  timer && clearTimeout(timer);
  timermon && clearTimeout(timermon);
  timer = setTimeout(() => {
    next();
    timermon = setTimeout(() => {
      animatestore && animatestore.SetAnimate(true);
    }, 500);
  }, 200);
});
router.afterEach((to, from) => {
  if (to.meta.title) {
    document.title = to.meta.title as string;
  } else {
    document.title = "AIOT";
  }
});
export default router;
